package switchviz.graphic.signal;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;

import switchviz.core.ResourceManager;
import switchviz.gui.component.JLabeldComponent;

public class NewSignalDialog extends JDialog implements ActionListener
{

	/**
	 * 
	 */
	private static final long serialVersionUID = -1255554002590080373L;
	private JLabeldComponent _txtsigname = null;
	private JLabeldComponent _cmbsigtype = null;
	private JButton _buttaddsignal = null;
	private JButton _buttcancel = null;
	private static NewSignalDialog _instance = null;
	
	private NewSignalDialog()
	{
		this.setSize(new Dimension(500, 100));
		this.setLocationRelativeTo(null);
		this.setVisible(true);
		initialize();
	}
	
	public static NewSignalDialog getInstance()
	{
		if(_instance != null)
			_instance.setVisible(false);
		_instance = new NewSignalDialog();
		return _instance;
	}
	
	private void initialize()
	{
		this.setLayout(ResourceManager.getInstance().getBorderLayout());
		
		_txtsigname = new JLabeldComponent("Name: ", JLabeldComponent.TEXTFIELD);
		_txtsigname.getTextField().setColumns(10);
		_cmbsigtype = new JLabeldComponent("Type: ", JLabeldComponent.COMBOBOX);
		_cmbsigtype.getComboBox().addItem(BasicSignalDataModel.TYPESIMPLE);
		_cmbsigtype.getComboBox().addItem(BasicSignalDataModel.TYPECOMPLEX);
		_cmbsigtype.getComboBox().addItem(BasicSignalDataModel.TYPETEST);
		_buttaddsignal = new JButton("Add");
		_buttaddsignal.setActionCommand(ResourceManager.getInstance().getActionAddItem());
		_buttcancel = new JButton("Cancel");
		_buttcancel.setActionCommand(ResourceManager.getInstance().getActionCancel());
		_buttcancel.addActionListener(this);
		
		JPanel addsigpanel = new JPanel();
		addsigpanel.setLayout(new BoxLayout(addsigpanel, BoxLayout.LINE_AXIS));
		addsigpanel.add(new JLabel("Add new Signal: "));
		addsigpanel.add(_txtsigname);
		addsigpanel.add(_cmbsigtype);
		
		JPanel buttpanel = new JPanel();
		buttpanel.add(_buttaddsignal);
		buttpanel.add(_buttcancel);

		this.add(addsigpanel, BorderLayout.CENTER);
		this.add(buttpanel, BorderLayout.SOUTH);
	}
	
	public void addActionListener(ActionListener l)
	{
		_buttaddsignal.addActionListener(l);
	}
	
	public String getSigType()
	{
		return _cmbsigtype.getText();
	}
	
	public String getSigName()
	{
		return _txtsigname.getText();
	}
	
	
	public void actionPerformed(ActionEvent e) 
	{
		this.setVisible(false);
	}

}
